<template>
  <div>
    <div class="search">
      <input v-model="keyword" class="search-input" type="text" placeholder="输入城市名或拼音" />
    </div>
    <div class="search-content" ref="search" v-show="keyword">
      <li class="search-item border-bottom" v-for="item in list" :key="item.id">{{item.name}}</li>
      <li class="search-item border-bottom" v-show="hasNoDate">没有找到匹配数据</li>
    </div>
  </div>
</template>
<script>
import Bscroll from "better-scroll";
export default {
  props: ["cities"],
  data() {
    return {
      keyword: "",
      list: [],
      timer: null
    };
  },
  computed: {
    hasNoDate() {
      return !this.list.length;
    }
  },
  watch: {
    keyword() {
      console.log(this.keyword);
      console.log(this.timer);
      if (this.timer) {
        console.log(this.keyword);
        clearTimeout(this.timer);
      }
      if (!this.keyword) {
        this.list = [];
        console.log(!this.keyword);
        return;
      }
      this.timer = setTimeout(() => {
        console.log(this.keyword + "---");
        console.log(this.cities);
        const result = [];
        for (let i in this.cities) {
          console.log(i);
          this.cities[i].forEach(value => {
            console.log(value);
            if (
              value.spell.indexOf(this.keyword) > -1 ||
              value.name.indexOf(this.keyword) > -1
            ) {
              result.push(value);
            }
          });
        }
        this.list = result;
      }, 100);
    }
  },
  mounted() {
    this.scroll = new Bscroll(this.$refs.search);
  }
};
</script>
<style lang="scss" scoped>
.search {
  //   //   width: 100%;
  //   height: 80px;
  //   //   padding-top: 80px;
  //   background: lightblue;

  .search-input {
    box-sizing: border-box;
    width: 90%;
    height: 30px;
    font-size: 15px;
    line-height: 20px;
    text-align: center;
    border-radius: 5px;
    color: #666666;
    position: relative;
    top: 10px;
    left: 5%;
  }
}
.search-content {
  z-index: 1;
  height: 623px;
  //   position: absolute;
  //   overflow: hidden;
  left: 0;
  right: 0;
  bottom: 0;
  background: #eee;
  position: relative;
  .search-item {
    line-height: 40px;
    border-bottom: 1px solid #eee;
    // padding-left: 5px;
    background: #ffffff;
    color: black;
    list-style: none;
    padding-left: 5%;
    position: relative;
  }
}
</style>